﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartSapce.ServiceLocation;
using SmartSpace.Core;

namespace SmartSpace.News
{
    public class NewsService : INewsService
    {
        /// <summary>
        /// inert or update
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public ServiceResult SaveArticle(Article article)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                if (context.Set<Article>().Where(p => p.Id == article.Id).Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    // article not exist in db
                    context.Set<Article>().Add(article);
                }
                else
                {
                    context.Entry(article).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true, "save article success!");
                }
                else
                {
                    Logger.Debug("news", string.Format("save article {0},{1} failure", article.Id, article.Title));
                    return new ServiceResult(false, "error");
                }
            }
        }


        public ServiceResult DeleteArticle(Guid id)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                Article article = context.Set<Article>().Where(p => p.Id == id).SingleOrDefault();
                if (article != null)
                {
                    context.Entry(article).State = System.Data.Entity.EntityState.Deleted;
                    if (context.SaveChanges() > 0)
                    {
                        return new ServiceResult(true, "delete article success!");
                    }
                    else
                    {
                        Logger.Debug("news", string.Format("delete article {0},{1} failure", article.Id, article.Title));
                        return new ServiceResult(false, "error");
                    }
                }

                return new ServiceResult(true, "delete article success, article is not in the db!");
            }
        }

        public SingleResult<Article> GetArticle(Guid id)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                Article article = context.Set<Article>().Where(p => p.Id == id).SingleOrDefault();
                if (article != null)
                {
                    return new SingleResult<Article>(article);
                }
                return new SingleResult<Article>(false, "error", null);
            }
        }

        public ServiceResult SaveCategory(Category category)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                if (context.Set<Category>().Where(p => p.Id == category.Id).Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    // category not exist in db
                    context.Set<Category>().Add(category);
                }
                else
                {
                    context.Entry(category).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true, "save category success!");
                }
                else
                {
                    Logger.Debug("news", string.Format("save category {0},{1} failure", category.Id, category.Name));
                    return new ServiceResult(false, "error");
                }
            }
        }

        public ServiceResult DeleteCategory(Guid id)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                Category category = context.Set<Category>().Where(p => p.Id == id).SingleOrDefault();
                if (category != null)
                {
                    context.Entry(category).State = System.Data.Entity.EntityState.Deleted;
                    if (context.SaveChanges() > 0)
                    {
                        return new ServiceResult(true, "delete category success!");
                    }
                    else
                    {
                        Logger.Debug("news", string.Format("delete category {0},{1} failure", category.Id, category.Name));
                        return new ServiceResult(false, "error");
                    }
                }

                return new ServiceResult(true, "delete category success, category is not in the db!");
            }
        }

        public SingleResult<Category> GetCategory(Guid id)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                Category category = context.Set<Category>().Where(p => p.Id == id).SingleOrDefault();
                if (category != null)
                {
                    return new SingleResult<Category>(category);
                }
                return new SingleResult<Category>(false, "error", null);
            }
        }


        public ListResult<Category> GetCategories()
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                return new ListResult<Category>(context.Set<Category>().ToList());
            }
        }
    }
}
